package com.txf.simple.ui.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.support.v4.content.ContextCompat;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.TextView;
import android.widget.ViewFlipper;

import com.txf.simple.R;
import com.txf.simple.tools.Logger;
import com.txf.simple.utils.UIUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by sunfusheng on 16/5/31.
 */
public class MarqueeView extends ViewFlipper {

    private Context mContext;
    private List<String> notices;
    private boolean isSetAnimDuration = false;

    private int interval = 2000;
    private int animDuration = 500;
    private int textSize = 14;
    private int textColor = 0xff313131;

    public MarqueeView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs, 0);
    }

    private void init(Context context, AttributeSet attrs, int defStyleAttr) {
        this.mContext = context;
        setPadding(UIUtils.dp2px(10),0,0,0);
        if (notices == null) {
            notices = new ArrayList<>();
        }

        TypedArray typedArray = getContext().obtainStyledAttributes(attrs, com.sunfusheng.marqueeview.R.styleable.MarqueeViewStyle, defStyleAttr, 0);
        interval = typedArray.getInteger(com.sunfusheng.marqueeview.R.styleable.MarqueeViewStyle_mvInterval, interval);
        isSetAnimDuration = typedArray.hasValue(com.sunfusheng.marqueeview.R.styleable.MarqueeViewStyle_mvAnimDuration);
        animDuration = typedArray.getInteger(com.sunfusheng.marqueeview.R.styleable.MarqueeViewStyle_mvAnimDuration, animDuration);
//        if (typedArray.hasValue(com.sunfusheng.marqueeview.R.styleable.MarqueeViewStyle_mvTextSize)) {
//            textSize = (int) typedArray.getDimension(com.sunfusheng.marqueeview.R.styleable.MarqueeViewStyle_mvTextSize, textSize);
//            textSize = DisplayUtil.px2sp(mContext, textSize);
//        }
//        textColor = typedArray.getColor(com.sunfusheng.marqueeview.R.styleable.MarqueeViewStyle_mvTextColor, textColor);
        typedArray.recycle();

        setFlipInterval(interval);

        Animation animIn = AnimationUtils.loadAnimation(mContext, com.sunfusheng.marqueeview.R.anim.anim_marquee_in);
        if (isSetAnimDuration) animIn.setDuration(animDuration);
        setInAnimation(animIn);

        Animation animOut = AnimationUtils.loadAnimation(mContext, com.sunfusheng.marqueeview.R.anim.anim_marquee_out);
        if (isSetAnimDuration) animOut.setDuration(animDuration);
        setOutAnimation(animOut);
    }
    // 根据公告字符串列表启动轮播
    public void startWithList(List<String> notices) {
        setNotices(notices);
        start();
    }
    // 启动轮播
    public boolean start() {
        if (notices == null || notices.size() == 0) return false;
        removeAllViews();
        for (String notice:notices) {
            TextView view = createTextView(notice);
            view.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View view) {
                    Logger.i("点击 :   "+((TextView)view).getText().toString());
                }
            });
            addView(view);
        }
        if (notices.size() > 1) {
            startFlipping();
        }
        return true;
    }
    // 创建ViewFlipper下的TextView
    private TextView createTextView(String text) {
        TextView tv = new TextView(mContext);
//        tv.setCompoundDrawablesRelativeWithIntrinsicBounds();
        tv.setCompoundDrawablePadding(UIUtils.dp2px(10));
        tv.setCompoundDrawablesWithIntrinsicBounds(ContextCompat.getDrawable(getContext(), R.drawable.hom_icon),null,null,null);
        tv.setGravity(Gravity.LEFT|Gravity.CENTER_VERTICAL);
        tv.setText(text);
        tv.setTextColor(textColor);
        tv.setTextSize(textSize);
        return tv;
    }
    public List<String> getNotices() {
        return notices;
    }
    public void setNotices(List<String> notices) {
        this.notices = notices;
    }

}
